package com.calvin.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.calvin.study.service.ISeckillService;

import cloud.alibaba.study.entity.base.ResponseVo;

@Controller
@RequestMapping("seckill")
public class SeckillController {
	@Autowired
	private ISeckillService seckillService;

	@GetMapping("getProducts")
	public @ResponseBody ResponseVo getProducts() {
		try {
			return ResponseVo.success(seckillService.getCurrentSeckillProducts());
		} catch (Exception e) {
			return ResponseVo.failure(e.getMessage());
		}
	}

	@GetMapping("getProducts/{seckillActId}/{productId}")
	public @ResponseBody ResponseVo getProducts(@PathVariable Long seckillActId,@PathVariable Long productId) {
		try {
			return ResponseVo.success(seckillService.getSeckillProductDetail(seckillActId, productId));
		} catch (Exception e) {
			return ResponseVo.failure(e.getMessage());
		}
	}

	@GetMapping("/doSeckill")
    public @ResponseBody ResponseVo doSeckill(@RequestParam("killId") String killId,
    			@RequestParam("randomCode") String randomCode,
    			@RequestParam("num") Integer num) {
    	try {
    			String orderNo= seckillService.doSeckill(killId, randomCode, num);
    	        return ResponseVo.success(orderNo);
		} catch (Exception e) {
			return ResponseVo.failure(e.getMessage());
		}
       
    }


}
